propsage.namespace("prestige.adsControl");

prestige.adsControl.adsToReview = (function($, ui){
	
	function initComponents() 
	{
		$('span.ad-approval-status.rejected').closest('tr').find('input:checkbox').hide();
		$('#reject-action-form').toggle($('table.items tr:gt(0) input:checkbox:visible').length > 0);
		$('#reject-advertisements-grid tbody').highlight(phrase);
	}
	
	
	function initEventHandlers() 
	{ 
		$('table.items input:checkbox').change(onCheckBoxChange);
		$('#back').click(onBtnBackClick);
	}
	
	function onBtnBackClick()
	{
		//		console.log('Btn Clicked');
		//		ui.doAjax('GET', 
		//			baseUrl + '/manage/modules/adsControl/ajaxRecheckCurrentAdvertisements', 
		//			{}, 
		//			onRecheckSuccess);

		window.location.href = baseUrl + '/manage/modules/adsControl/allFilters';
	}
	
	function onRecheckSuccess(response)
	{
	//		if(response.remaining > 0){
	//			var activeCount = response.active;
	//			var pendingCount = response.pending;
	//			var phrase = response.phrase;
	//			var message = 'For the phrase "' + phrase + '", you still have : \n' ;
	//			message += ' \t' + activeCount + ' ad/s already approved for launch \n';
	//			message += ' \t' + pendingCount + ' ad/s awaiting approval \n';
	//			message += 'Click OK if no further rejections are necessary.';
	//			var result = confirm(message); 
	//			if(!result){
	//				return false;
	//			} else {
	//				window.location.href = baseUrl + '/manage/modules/adsControl/allFilters';
	//			}
	//		} else {
	//			window.location.href = baseUrl + '/manage/modules/adsControl/allFilters';
	//		}
		
	}
	
	function onCheckBoxChange()
	{
		if($('table.items tr:gt(0) input:checkbox:visible').length > 0 && $('table.items tr:gt(0) input:checkbox:checked:visible').length == $('table.items tr:gt(0) input:checkbox:visible').length){
			$('#reject-advertisements-grid_cid_all').prop('checked',true);
		}
		
		if($('table.items tr:gt(0) input:checkbox:checked:visible').length > 0){
			$('input:submit[name="reject_action_confirm"]').addClass('blue').removeAttr('disabled');
		} else {
			$('input:submit[name="reject_action_confirm"]').removeClass('blue').attr("disabled", "disabled");
		}
	}
	
	$(document).ready(function(){
		initComponents();
		initEventHandlers();
	});
    
}(jQuery, propsage.ui));